package com.m4f.appengine.utils.web.bind;

import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.beans.propertyeditors.CustomBooleanEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.beans.propertyeditors.CustomCollectionEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.context.request.WebRequest;
import com.google.appengine.api.datastore.GeoPt;
import com.google.appengine.api.datastore.Text;


public class BindingInitializer extends ConfigurableWebBindingInitializer  {
	
	private String dateFormat = "dd/MM/yyyy";
	
	public void initBinder(WebDataBinder binder, WebRequest request) {        
		/*Necessary to run into GAE.*/
		binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
		binder.registerCustomEditor(Boolean.class, new CustomBooleanEditor("true", "false", true));
		binder.registerCustomEditor(Long.class, null, new CustomNumberEditor(Long.class, null, true));
        binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, null, true));
        binder.registerCustomEditor(Float.class, null, new CustomNumberEditor(Float.class, null, true));
        binder.registerCustomEditor(List.class, new CustomCollectionEditor(List.class, true));
        binder.registerCustomEditor(Text.class, new CustomTextEditor());
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(dateFormat), true));
        binder.registerCustomEditor(GeoPt.class, new CustomGeoPointsEditor());
        /*To validate and more actions. */
        super.initBinder(binder, request);
	}
	
	public void setDateFormat(String dFormat) {
		this.dateFormat = dFormat;
	}
	
	public String getDateFormat() {
		return this.dateFormat;
	}
	
	/**
	 * Example of custom binder to com.google.appengine.api.datastore.GeoPt.class
	 *
	 */
	private class CustomGeoPointsEditor extends PropertyEditorSupport {
		
		@Override
		public String getAsText() {
			StringBuffer sb = new StringBuffer();
			if(getValue() != null) {
				GeoPt geoPt = (GeoPt) getValue();
				if(geoPt != null) {
					sb.append(geoPt.getLatitude()).append(",").append(geoPt.getLongitude());
				}
			}
			return sb.toString();
		}
		
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			if(text != null && !("").equals(text)) {
				String[] coords = text.split(",");
				if(coords != null && coords.length == 2) {
					float latitude = Float.parseFloat(coords[0]);
					float longitude = Float.parseFloat(coords[1]);
					GeoPt geoPt = new GeoPt(latitude, longitude);
					setValue(geoPt);
				}
			}
		}
	}
	

	private class CustomTextEditor extends PropertyEditorSupport {	
		public String getAsText() {
			String value = getValue() != null ? ((Text)getValue()).getValue() : "";
			return value;
		}
		
		public void	setAsText(String text) {
			System.out.println();
			setValue(new Text(text));
		}
	}
	
		
}